﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Windows.Interop;

namespace HMS.ClassLibrary
{
    [StructLayout(LayoutKind.Sequential)]
    public struct MARGINS
    {
        public int cxLeftWidth;      // width of left border that retains its size
        public int cxRightWidth;     // width of right border that retains its size
        public int cyTopHeight;      // height of top border that retains its size
        public int cyBottomHeight;   // height of bottom border that retains its size
    };

    /// <summary>
    /// Enables the vista glass effect for forms
    /// </summary>
    public class GlassEffect
    {
        [DllImport("DwmApi.dll")]
        public static extern int DwmExtendFrameIntoClientArea(
            IntPtr hwnd,
            ref MARGINS pMarInset);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr DefWindowProc(
            IntPtr hWnd,
            int msg,
            IntPtr wParam,
            IntPtr lParam);

        private const int WM_NCHITTEST = 0x0084;
        private const int HTBORDER = 18;
        private const int HTBOTTOM = 15;
        private const int HTBOTTOMLEFT = 16;
        private const int HTBOTTOMRIGHT = 17;
        private const int HTLEFT = 10;
        private const int HTRIGHT = 11;
        private const int HTTOP = 12;
        private const int HTTOPLEFT = 13;
        private const int HTTOPRIGHT = 14;

        private IntPtr WindowPtr;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="windowPtr">IntPtr to the window object</param>
        public GlassEffect(IntPtr windowPtr)
        {
            this.WindowPtr = windowPtr;
        }
        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            // Override the window hit test
            // and if the cursor is over a resize border,
            // return a standard border result instead.
            if (msg == WM_NCHITTEST)
            {
                handled = true;
                var htLocation = DefWindowProc(hwnd, msg, wParam, lParam).ToInt32();
                switch (htLocation)
                {
                    case HTBOTTOM:
                    case HTBOTTOMLEFT:
                    case HTBOTTOMRIGHT:
                    case HTLEFT:
                    case HTRIGHT:
                    case HTTOP:
                    case HTTOPLEFT:
                    case HTTOPRIGHT:
                        htLocation = HTBORDER;
                        break;
                }

                return new IntPtr(htLocation);
            }

            return IntPtr.Zero;
        }
        public void EnableGlassEffect()
        {
            try
            {
                // Obtain the window handle for WPF application
                HwndSource MainWindowSrc = HwndSource.FromHwnd(this.WindowPtr);
                MainWindowSrc.CompositionTarget.BackgroundColor = Color.FromArgb(0, 0, 0, 0);
                MainWindowSrc.AddHook(WndProc);

                // Set Margins
                MARGINS Margins = new MARGINS();
                Margins.cxLeftWidth = 10;
                Margins.cxRightWidth = 10;
                Margins.cyBottomHeight = 10;
                Margins.cyTopHeight = 10;

                int hr = DwmExtendFrameIntoClientArea(MainWindowSrc.Handle, ref Margins);
                //
                if (hr < 0)
                {
                    //DwmExtendFrameIntoClientArea Failed
                }
            }
            // If not Vista, paint background white.
            catch (DllNotFoundException)
            {

            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
    }
}
